package com.sls.web.manager.recharge.service.impl;

import java.lang.reflect.Type;
import java.util.UUID;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.PTConstants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.PriceTemplate;
import com.sls.web.manager.recharge.querybean.PriceTemplateQuery;
import com.sls.web.manager.recharge.service.PriceTemplateService;
import com.sls.web.manager.socket.SuperService;

/**
 * @author sxf
 * 
 */
@Service("priceTemplateService")
public class PriceTemplateServiceImpl extends SuperService implements
		PriceTemplateService {

	/**
	 * @param serviceCode
	 * @param sendObj
	 * @param userInfoVO
	 * @param classType
	 *            类型信息(如:String,为String.class)
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public <T> T callService(String serviceCode, Head head, Object sendObj,
			UserInfoVO userInfoVO, Object... classType) throws Exception {
		Gson gson = CommonUtil.gson();
		if (head == null) {
			head = new Head();
			head.setRequestId(UUID.randomUUID().toString());
			head.setServiceCode(serviceCode);
			head.setIp(userInfoVO.getCurrentIp());
			head.setUserId(userInfoVO.getUserId());
		}
		SocketDTO requestDTO = super.send(head, sendObj);
		if (!TransmissionCode.SUCCESS.getCode().equals(
				requestDTO.getHead().getResponseCode())) {
			throw new ManagerException(requestDTO.getHead()
					.getResponseMessage());
		}
		String body = requestDTO.getBody();
		T t = null;
		if (body != null) {
			if (classType != null && classType.length > 0) {
				if (classType[0] instanceof Class) {
					t = gson.fromJson(body, (Class<T>) classType[0]);
				} else if (classType[0] instanceof Type) {
					t = gson.fromJson(body, (Type) classType[0]);
				}
			}
		}
		return t;
	}

	@Override
	public String save(PriceTemplate pt, UserInfoVO userinfo) throws Exception {
		this.callService(PTConstants.INSERT, null, pt, userinfo);
		return null;
	}

	@Override
	public void delete(String id, UserInfoVO userinfo) throws Exception {
		PriceTemplate pt = new PriceTemplate();
		pt.setId(id);
		this.callService(PTConstants.DELETE_BY_ID, null, pt, userinfo);
	}

	@Override
	public void update(PriceTemplate pt, UserInfoVO userinfo) throws Exception {
		this.callService(PTConstants.UPDATE_BY_ID, null, pt, userinfo);
	}

	@Override
	public PriceTemplate findById(String id, UserInfoVO userinfo)
			throws Exception {
		PriceTemplateQuery pt = new PriceTemplateQuery();
		pt.setId(id);
		PriceTemplate pTemp = this.callService(PTConstants.FIND_BY_ID, null,
				pt, userinfo, PriceTemplate.class);
		return pTemp;
	}

	@Override
	public Pagination<PriceTemplate> findPage(PriceTemplateQuery ptq,
			UserInfoVO userInfo) throws Exception {
		if (ptq == null) {
			ptq = new PriceTemplateQuery();
			ptq.setCurrentPage(1);
			ptq.setPageSize(5);
		}
		return this.callService(PTConstants.FIND_PAGE, null, ptq, userInfo,
				new TypeToken<Pagination<PriceTemplate>>() {
				}.getType());
	}

}
